package cn.caojun.auth.service.Impl;

import cn.caojun.auth.annotation.caojunPermission;
import cn.caojun.auth.domain.Permission;
import cn.caojun.auth.mapper.PermissionMapper;
import cn.caojun.auth.query.PermissionQuery;
import cn.caojun.auth.service.IPermissionScanService;
import cn.caojun.basic.utils.ClassUtils;
import cn.caojun.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@Service
public class PermisssionService implements IPermissionScanService {

    @Value("${caojun.permission.scan-base-package}")
    private String permissionScanPackage;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public void scan() {

        // 1.拿到所有的controller类，自定义注解只会用在controller类和他的方法上
        List<Class> allClassName= ClassUtils.getAllClassName(permissionScanPackage);
        permissionMapper.deleteAll();
        // 2.判断controller类上是否有@CaojunPermission
        for (Class aClass : allClassName) {
            caojunPermission annotation =(caojunPermission) aClass.getAnnotation(caojunPermission.class);

            if (Objects.isNull(annotation)){
                // 3.如果没有就跳过不处理
                continue;
            }
            RequestMapping requestMapping =(RequestMapping) aClass.getAnnotation(RequestMapping.class);
            String classUrl=requestMapping.value()[0];
            Permission parent=new Permission();
            parent.setName(annotation.name());
            parent.setDescs(annotation.desc());
            parent.setUrl(classUrl);
            parent.setSn(aClass.getSimpleName());
            permissionMapper.insert(parent);

                // 4.如果有就拿到类里面的所有方法
                Method[] methods = aClass.getMethods();
                for (Method method : methods) {
                    // 5.循环所有的方法，判断方法上面是否有@CaojunPermission
                    caojunPermission cPermission=method.getAnnotation(caojunPermission.class);

                    if (Objects.isNull(cPermission)){
                        continue;
                        // 6.若方法上没有则跳过不处理
                    }
                    // 7.若有则就对其上边的@CaojunPermission进行解析，并封装Permisssion对象
                    Permission permission=new Permission();
                    String name=cPermission.name();
                    String desc=cPermission.desc();
                    String url=classUrl+getUrl(aClass,method);
                    String sn=aClass.getSimpleName()+":"+method.getName();
                    permission.setName(name);
                    permission.setDescs(desc);
                    permission.setUrl(url);
                    permission.setSn(sn);
                    permission.setParentId(parent.getId());
                    permissionMapper.insert(permission);
                }

        }

        // 8.封装的Permission对象保存至数据库中
    }

    private String getUrl(Class<?> clasz,Method method){
        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
        StringBuffer url=new StringBuffer();
        if (annotation!=null)
        {
            String path = annotation.value().length>0? annotation.value()[0]:"";
            url.append(path);
        }

        GetMapping annotation1 = method.getAnnotation(GetMapping.class);
        if (annotation1!=null){
            String path = annotation1.value().length>0? annotation1.value()[0]:"";
            url.append(path);
        }

        PostMapping annotation2 = method.getAnnotation(PostMapping.class);
        if (annotation2!=null){
            String path = annotation2.value().length>0? annotation2.value()[0]:"";
            url.append(path);
        }

        PatchMapping annotation3 = method.getAnnotation(PatchMapping.class);
        if (annotation3!=null){
            String path = annotation3.value().length>0? annotation3.value()[0]:"";
            url.append(path);
        }

        DeleteMapping annotation4 = method.getAnnotation(DeleteMapping.class);
        if (annotation4!=null){
            String path = annotation4.value().length>0? annotation4.value()[0]:"";
            url.append(path);
        }
        return url.toString();
    }



}
